package apof.util.xml
{
	import flash.net.URLLoader;
	import mx.rpc.http.HTTPService;
	import mx.rpc.http.HTTPService;
	import mx.rpc.events.ResultEvent;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.xml.XMLDocument;
	import apof.model.User;
	import mx.collections.ArrayCollection;
	import flash.events.EventDispatcher;
	import mx.utils.ObjectProxy;
	import flash.utils.describeType;
	import mx.controls.Alert;
	import flash.events.IOErrorEvent;
	import mx.managers.CursorManager;
	
	public class XmlParser extends EventDispatcher
	{
		private var id:String;
		private var name:String;
		private var passwd:String;
		private var imgUrl:String;
		private var location:String;
		private var friends_count:String;
		private var status_text:String;
		private var status_created_at:String;
		private var description:String;
		private var status_id:String;
		private var status_count:String;
		
		private var friends:ArrayCollection;
		
		public function XmlParser(userName:String):void {
			trace("start getting user profile:"+userName);
			var loader:URLLoader = new URLLoader;
			loader.addEventListener(IOErrorEvent.IO_ERROR,ioErr);
			loader.addEventListener(Event.COMPLETE,pasreUserXml);
			loader.load(new URLRequest("http://api.fanfou.com/users/show/"+userName+".xml"));
		}
	
	    private function ioErr(e:Event):void{
//	    	CursorManager.removeAllCursors()
            Alert.show("网络传输错误","错误信息",4,null);
        }
		
		public function pasreUserXml(e:Event):void{
			
	
			var userXml:XML = new XML(e.currentTarget.data);
			
			trace("Get user completed");
			
//			id = userXml.child("id").toString();
//			name = userXml.child("name").toString();
//			description = userXml.child("description").toString();
//			imgUrl = userXml.child("profile_image_url").toString();
//			location = userXml.child("location").toString();
//			friends_count = userXml.child("friends_count").toString();
//			status_text = userXml.child("status").child("text").toString();
//			status_created_at = userXml.child("status").child("created_at").toString();
//			status_id = userXml.child("status").child("id").toString();
//			status_count = userXml.child("statuses_count").toString();

			id = userXml.id;
			name = userXml.name;
			imgUrl = userXml.profile_image_url;
			location = userXml.location;
			description = userXml.description;
			friends_count = userXml.friends_count;
			status_text = userXml.status.text;
			status_created_at = userXml.status.created_at;
			status_id = userXml.status.id;
			status_count = userXml.statuses_count;
				
			getFriendsXml();
		}
		
		protected function getFriendsXml():void {
			var loader:URLLoader = new URLLoader;
			loader.addEventListener(Event.COMPLETE,parseFriendsXml);
			loader.load(new URLRequest("http://api.fanfou.com/users/friends/"+id+".xml"));
		}
		
		public function parseFriendsXml(e:Event):void{
			var friendsXml:XML = new XML(e.currentTarget.data);
			var array:Array = new Array;
			
			for(var i:int=0;i<friendsXml.children().length();i++)
			{
//				var id:String=friendsXml.user[i].child("id").toString();
//				var name:String=friendsXml.user[i].child("name").toString();
//				var description:String=friendsXml.user[i].child("description").toString();
//				var imgUrl:String=friendsXml.user[i].child("profile_image_url").toString();
//				var location:String=friendsXml.user[i].child("location").toString();
//				array.push({userID:id,des:description,img:imgUrl,name:name,location:location});
//				trace("id:"+id+" des:"+description+" img:"+imgUrl+" name:"+name+" location:"+location);

				var id:String=friendsXml.user[i].id;
				var name:String=friendsXml.user[i].name;
				var description:String=friendsXml.user[i].description;
				var imgUrl:String=friendsXml.user[i].profile_image_url;
				var location:String=friendsXml.user[i].location;
				array.push({userID:id,des:description,img:imgUrl,name:name,location:location});
			}
			
			friends = new ArrayCollection(array);
			applyObjectProxy(friends);
			this.dispatchEvent(new Event(Event.COMPLETE));
			
		}
		
		public function applyObjectProxy(targetAC:ArrayCollection):ArrayCollection
		{
			for(var i:String in targetAC)
			{targetAC[i] = new ObjectProxy(targetAC[i]);}
			return targetAC;
		}
		

		
		public function getId ():String {return id;}
		public function getImgUrl():String { return imgUrl;}
		public function getName():String {return name;}
		public function getFriends():ArrayCollection{return friends;}
		public function getLocation():String { return location;}
		public function getFriendsCount():String { return friends_count;}
		public function getStatusText():String{return status_text;}
		public function getStatusCreatedAt():String{return status_created_at;}
		public function getDescription():String{return description;}
		public function getStatusId():String{return status_id;}
		public function getStatusCount():String{return status_count;}
		
	

	}
	

}